package cn.chi.order.service.impl;

import cn.chi.base.constant.OrderTypeConstants;
import cn.chi.base.service.impl.BaseServiceImpl;
import cn.chi.order.domain.OrderAddress;
import cn.chi.order.domain.OrderAdopt;
import cn.chi.order.mapper.OrderAddressMapper;
import cn.chi.order.service.IOrderAdoptService;
import cn.chi.pay.domain.AlipayInfo;
import cn.chi.pay.domain.PayBill;
import cn.chi.pay.mapper.AlipayInfoMapper;
import cn.chi.pay.mapper.PayBillMapper;
import cn.chi.pay.service.IPayService;
import cn.chi.pet.domain.Pet;
import cn.chi.pet.mapper.PetMapper;
import cn.chi.user.domain.User;
import cn.chi.user.domain.UserAddress;
import cn.chi.user.mapper.UserAddressMapper;
import cn.chi.util.CodeGenerateUtils;
import cn.chi.util.LoginContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
@Service
public class OrderAdoptServiceImpl extends BaseServiceImpl<OrderAdopt> implements IOrderAdoptService {
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private PetMapper petMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private AlipayInfoMapper alipayInfoMapper;
    @Autowired
    private IPayService payService;

    @Override
    public String createOrder(Map<String, String> map, HttpServletRequest request) {
        //配送地址
        Long address_id = Long.valueOf(map.get("address_id"));
        //领养的宠物
        Long pet_id = Long.valueOf(map.get("pet_id"));
        //支付方式
        String payMethod = map.get("payMethod");
        //1.根据用户选择的配送地址 查询 UserAddress
        UserAddress userAddress = userAddressMapper.queryById(address_id);
        //2.根据领养的宠物id Pet对象 查询出来
        Pet pet = petMapper.queryById(pet_id);
        //3.根据 pet对象 生成一个 OrderAdopt对象出来 保存
        OrderAdopt orderAdopt =pet2OrderAdopt(pet,request);
        super.save(orderAdopt);//返回新增数据主键
        //4.根据用户的地址 生成一个  OrderAddress对象 保存
        OrderAddress orderAddress = userAddress2OrderAddress(userAddress);
        orderAddress.setOrderSn(orderAdopt.getOrderSn());
        orderAddress.setOrder_id(orderAdopt.getId());
        //返回新增数据的主键
        orderAddressMapper.save(orderAddress);
        //更新关联字段修改数据库
        orderAdopt.setAddress_id(orderAddress.getId());
        super.update(orderAdopt);
        //5.生成一个支付单
        PayBill payBill=orderAdopt2PayBill(orderAdopt);
        //补全 支付方式
        payBill.setPayChannel(payMethod);
        payBillMapper.save(payBill);
        //支付
        AlipayInfo alipayInfo = alipayInfoMapper.queryByShopId(payBill.getShop_id());
        String result =payService.pay(payBill,alipayInfo);
        System.out.println(result);
        return result;
    }

    /**
     * 生成支付订单数据转换
     * @param orderAdopt
     * @return
     */
    private PayBill orderAdopt2PayBill(OrderAdopt orderAdopt) {
        PayBill payBill = new PayBill();
        BeanUtils.copyProperties(orderAdopt,payBill);
        //对应的 订单的id
        payBill.setBusinessKey(orderAdopt.getId());
        payBill.setMoney(orderAdopt.getPrice());
        payBill.setBusinessType(OrderTypeConstants.ORDER_ADOPT);
        payBill.setCreateTime(new Date());
        return  payBill;
    }

    /**
     * 数据拷贝
     * @param userAddress
     * @return
     */
    private OrderAddress userAddress2OrderAddress(UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress,orderAddress);
        return orderAddress;
    }

    /**
     * 数据拷贝
     * @param pet
     * @param request
     * @return
     */
    private OrderAdopt pet2OrderAdopt(Pet pet, HttpServletRequest request) {
        OrderAdopt orderAdopt = new OrderAdopt();
        orderAdopt.setDigest("【宠物之家】-领养订单");
        orderAdopt.setState(0);//待支付状态
        orderAdopt.setPrice(pet.getSaleprice());
        //全局唯一不重复
        User current = (User) LoginContext.getCurrent(request);
        //生成订单号
        orderAdopt.setOrderSn(CodeGenerateUtils.generateOrderSn(current.getId()));

        orderAdopt.setLastPayTime(new Date(System.currentTimeMillis()+1000*60*15));
        orderAdopt.setPet_id(pet.getId());
        //获取当前的登录用户
        orderAdopt.setUser_id(current.getId());
//        private Long shop_id;
        orderAdopt.setShop_id(pet.getShop_id());

        return orderAdopt;
    }
}
